<template>
  <span>{{ text }}</span>
</template>

<script>
  import {filterDictText, getDictItemsFromCache} from '@/components/dict/JDictSelectUtil'
  export default {
    name: 'JDictTag',
    props: {
      dictCode: {
        type: String,
        required: true
      },
      value: {
        type: [String, Number, Array],
        required: true
      }
    },
    data () {
      return {
        dictOptions: []
      }
    },
    computed: {
      text () {
        if (!this.value) {
          return ''
        }
        if (this.dictOptions && this.dictOptions.length > 0) {
          return filterDictText(this.dictOptions, this.value)
        }
        return this.value
      }
    },
    watch: {
      dictCode: {
        immediate: true,
        handler() {
          this.initDictData()
        }
      }
    },
    methods: {
      initDictData() {
        // 优先从缓存中读取字典配置
        if (getDictItemsFromCache(this.dictCode)) {
          this.dictOptions = getDictItemsFromCache(this.dictCode)
          return
        }
      }
    }
  }
</script>

<style scoped>
</style> 